package com.teamin.service;

import com.teamin.config.ErrorCode;
import com.teamin.web.exception.GlobalErrorInfoException;
import com.teamin.web.model.ErrorInfo;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class NameRuleService {

	/**
	 * 判断评论
	 * 
	 * @param request
	 * @throws GlobalErrorInfoException
	 */
	public void taskCommentRule(String content, boolean request) throws GlobalErrorInfoException {
		// 不为空或空字符串
		if (content != null && !content.equals("")) {
//			if (content.length() > 200) {
//				throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.RULE_COMMENT));
//			}
		}
		// 要求不能为空
		else if (request) {
			throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.RULE_COMMENT));
		}
	}

	/**
	 * 判断圈子名合法
	 * 
	 * @param cirlceName 圈子的名称
	 * @throws GlobalErrorInfoException
	 */
	public void cricleNameRule(String cirlceName) throws GlobalErrorInfoException {
		//[0-9a-zA-Z\u4E00-\u9FA5\u3010\u3011\x5b\x5d\x2d\x5f]+
		String regex = "[0-9a-zA-Z\\u4E00-\\u9FA5\\u3010\\u3011\\x5b\\x5d\\x2d\\x5f]+";
		if (cirlceName != null && !cirlceName.equals("")) {
			Pattern pattern = Pattern.compile(regex);
			Matcher matcher = pattern.matcher(cirlceName);

			if (cirlceName.length() > 16 || !matcher.matches()) {
				throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.RULE_CIRCLENAME));
			}
		} else
			throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.RULE_CIRCLENAME));
		}
}
